package com.hsource.doctorcenter.manager.factory.callBackService;

import com.alibaba.fastjson.JSONObject;
import com.hsource.common.utils.bean.BeanUtils;
import com.hsource.doctorcenter.conf.dataSource.aspect.SnowflakeIdWorker;
import com.hsource.doctorcenter.domain.order.Order;
import com.hsource.doctorcenter.domain.order.OrderDoctor;
import com.hsource.doctorcenter.feign.PaymentFeignClient;
import com.hsource.doctorcenter.service.api.IApiService;
import com.hsource.doctorcenter.service.order.IOrderDoctorService;
import com.hsource.doctorcenter.service.order.IOrderService;
import com.hsource.doctorcenter.util.SpringUtils;
import com.hsource.doctorcenter.util.StringUtils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/10/17 21:43
 * @describe 定向电话回调处理服务
 */
public class DirectedPhoneCallBackService {


    /**
     * 回调数据解析、处理
     * @param callBackDataMap 回调数据
     */
    public void callBackDataService(Map<String,Object> callBackDataMap){
        IOrderService orderService = SpringUtils.getBean(IOrderService.class);
        IOrderDoctorService orderDoctorService = SpringUtils.getBean(IOrderDoctorService.class);
        IApiService apiService = SpringUtils.getBean(IApiService.class);
        if(callBackDataMap.size()>0) {
            OrderDoctor orderDoctor = orderDoctorService.selectOrderDoctorByProblemId(Integer.parseInt(callBackDataMap.get("service_id").toString()));
            if(StringUtils.isNotNull(orderDoctor)) {
                Order order = orderService.selectOrderById(orderDoctor.getOrderId());
                if(order.getUserId().equals(callBackDataMap.get("user_id").toString())) {
                    Order directedPhoneNewOrder = new Order();
                    BeanUtils.copyBeanProp(directedPhoneNewOrder, order);
                    if(callBackDataMap.containsKey("first_dial_time")) {
                        orderDoctor.setFirstDialTime(callBackDataMap.get("first_dial_time").toString());
                    }
                    if(callBackDataMap.containsKey("finish_time")) {
                        orderDoctor.setFinishTime(callBackDataMap.get("finish_time").toString());
                    }
                    orderDoctor.setProblemStatus(callBackDataMap.get("status").toString());
                    orderDoctor.setCallTime(Calendar.getInstance().getTime());
                    //正常结束
                    if (callBackDataMap.get("status").equals("phone_close")) {
                        orderDoctor.setCallDuration(Integer.parseInt(callBackDataMap.get("call_duration").toString()));
                        orderDoctorService.updateOrderDoctor(orderDoctor);
                    }
                    if (callBackDataMap.get("status").equals("phone_refund")) {
                        directedPhoneNewOrder.setPayStatus("T");
                        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 2);
                        directedPhoneNewOrder.setRefundOrderId(String.valueOf(idWorker.nextId()));
                        directedPhoneNewOrder.setRefundPrice(directedPhoneNewOrder.getPrice());
                        directedPhoneNewOrder.setRefundTime(Calendar.getInstance().getTime());
                        directedPhoneNewOrder.setRefundDesc(callBackDataMap.get("msg").toString());


                        orderDoctor.setIsRefund(1);
                        orderDoctor.setRefundTime(Calendar.getInstance().getTime());
                        orderDoctor.setRefundNo(directedPhoneNewOrder.getRefundOrderId());
                        orderDoctor.setRefundDesc(callBackDataMap.get("msg").toString());


                        /**退款参数组装 begin**/
                        Map<String, String> refundParamsMap = new HashMap<>();
                        refundParamsMap.put("transaction_id", directedPhoneNewOrder.getPayOrderId());
                        refundParamsMap.put("out_trade_no", directedPhoneNewOrder.getId());
                        refundParamsMap.put("out_refund_no", directedPhoneNewOrder.getRefundOrderId());
                        refundParamsMap.put("refund_desc",directedPhoneNewOrder.getRefundDesc());
                        refundParamsMap.put("total_fee", directedPhoneNewOrder.getPrice().toString());
                        refundParamsMap.put("refund_fee", directedPhoneNewOrder.getPrice().toString());
                        refundParamsMap.put("pay_type", directedPhoneNewOrder.getPayType());
                        PaymentFeignClient paymentFeignClient = SpringUtils.getBean(PaymentFeignClient.class);
                        /**退款参数组装 end**/
                        /**推送订单退款消息 begin**/
                        //调用退款接口
                        if (paymentFeignClient.refund(JSONObject.toJSONString(refundParamsMap)).getSuccess()) {
                            orderService.updateOrder(directedPhoneNewOrder);
                            orderDoctorService.updateOrderDoctor(orderDoctor);
                        }
                        /**推送订单退款消息 end**/
                    }
                }
            }
        }
    }
}
